package com.hj.security.engine;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;

import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.util.Xml;

import com.hj.security.domain.SmsInfo;

public class SmsService {

	private Context context;

	public SmsService(Context context) {
		this.context = context;
	}

	public List<SmsInfo> getSmsInfo() {
		List<SmsInfo> infos = new ArrayList<SmsInfo>();
		ContentResolver resolver = context.getContentResolver();
		Uri uri = Uri.parse("content://sms/");
		Cursor cursor = resolver.query(uri, new String[]{"_id", "address", "date", "type", "body"}, null, null, null);
		SmsInfo info;
		while (cursor.moveToNext()) {
			info = new SmsInfo();
			info.setId(cursor.getString(cursor.getColumnIndex("_id")));
			info.setAddress(cursor.getString(cursor.getColumnIndex("address")));
			info.setDate(cursor.getString(cursor.getColumnIndex("date")));
			info.setType(cursor.getInt(cursor.getColumnIndex("type")));
			info.setBody(cursor.getString(cursor.getColumnIndex("body")));
			infos.add(info);
		}
		cursor.close();
		return infos;
	}
	//还原
	public void restore(String path, ProgressDialog pd) throws Exception{
		File file = new File(path);
		FileInputStream fis = new FileInputStream(file);
		XmlPullParser parser = Xml.newPullParser();
		ContentValues values = null;
		parser.setInput(fis, "utf-8");
		int type = parser.getEventType();
		int index = 0;
		while (type != XmlPullParser.END_DOCUMENT) {
			switch (type) {
			case XmlPullParser.START_TAG:
				if("count".equals(parser.getName())){
					int count = Integer.parseInt(parser.nextText());
				}
				if("sms".equals(parser.getName())){
					values = new ContentValues();
				}else if("address".equals(parser.getName())){
					values.put("address", parser.nextText());
				}else if("date".equals(parser.getName())){
					values.put("date", parser.nextText());
				}else if("type".equals(parser.getName())){
					values.put("type", parser.nextText());
				}else if("body".equals(parser.getName())){
					values.put("body", parser.nextText());
				}
				break;
			case XmlPullParser.END_TAG:
				if("sms".equals(parser.getName())){
					ContentResolver resolver = context.getContentResolver();
					resolver.insert(Uri.parse("content://sms/"), values);
					values = null;
					index++;
					pd.setProgress(index);
				}
				break;
			default:
				break;
			}
			type = parser.next();
		}
	}
}
